<?php

/*
|--------------------------------------------------------------------------
| Routes File
|--------------------------------------------------------------------------
|
| Here is where you will register all of the routes in an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/
use Illuminate\Http\Request;
use App\Http\Controllers\SMSService;
use App\Http\Controllers\EmailService;
use Milon\Barcode\DNS1D;
use Milon\Barcode\DNS2D;
use App\Config;
use App\User;

$proxy_url = getenv('PROXY_URL');
if (!empty($proxy_url)) {
   URL::forceRootUrl($proxy_url);
}

Route::group(['middleware' => ['web']], function () {

    Route::get('/', ['as' => 'home', function (Request $request) {
        return view('welcome');
    }])->middleware('guest');

    Route::get('switchLanguage','UserController@switchLanguage');
    Route::get('linkPortal','UserController@linkPortal');
    Route::resource('users', 'UserController', ['only' => ['index']]);
    Route::auth();

    Route::get('template', function(Request $request) {
        User::setDefaultLocale($request);
         $name = $request->input('name');
        if (empty($name)) {
            $name = 'users.template';
        }
        if (!Auth::check() && !preg_match('/auth/', $name)) {
            return response('Unauthorized.', 401);
        }

        return view($name, ['user' => Auth::check() ? Auth::user() : []]);
    });
    

    Route::post('sendSMS', function(Request $request, SMSService $SMSService) {
        $code = '';
        for($i = 0; $i < 4; $i++) {
            $code .= rand(0, 9);
        }
        $phone = $request->input('phone');
        if (!empty($phone)) {
            $params = array('phone' => $phone);
            $params['message'] = '您的短信验证码为：'.$code;
        }

        if (config('app.debug')) {
            $request->session()->put('smscode', '1111');
            if (isset($params['phone'])) {
                $request->session()->put('smscode_method', 'phone');
            }
            return ;
        } else {
            $request->session()->put('smscode', $code);
        }

        if (isset($params['phone'])) {
            $abc = $SMSService->sendSMS($params);
            $request->session()->put('smscode_method', 'phone');
        }
    });

    Route::post('smsCodeValidate', function(Request $request) {
        $code_request = $request->input('sms_code');
        $sms_code = session('smscode');
        if (empty($code_request) || $code_request != $sms_code) {
            echo 0;
        } else {
            echo 1;
        }
    });

});
